package utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import service.OrgManager;
import action.vo.IdName;


public class IdNameUtils {
	
	public static String encode(List<IdName> idNameList){
		Set<IdName> l = new HashSet<IdName>();
		for(IdName idName : idNameList){
			l.add(idName);
		}
		String participant = "";
		for(IdName idName : l){
			participant += idName.getPrefix() + idName.getId()+ ";";
		}
		if(participant.endsWith(";"))
			participant = participant.substring(0,participant.length()-1);
		
		return participant;
	}
	
	public static List<IdName> decode(String participantList){
		
		String[] pl = participantList.split(";");
		List<IdName> idNameList = new ArrayList<IdName>();
		for(String  participant : pl){
			if(participant!=null&&!"".equals(participant)){
				if(participant.startsWith(OrgManager.ORGN_PREFIX)){
					IdName idName = new IdName(participant.substring(5),"",OrgManager.ORGN_PREFIX);
					idNameList.add(idName);
				}
				else if(participant.startsWith(OrgManager.ROLE_PREFIX)){
					IdName idName = new IdName(participant.substring(5),"",OrgManager.ROLE_PREFIX);
					idNameList.add(idName);
				}
				else if(participant.startsWith(OrgManager.USER_PREFIX)){
					IdName idName = new IdName(participant.substring(5),"",OrgManager.USER_PREFIX);
					idNameList.add(idName);
				}
				else if(participant.startsWith(OrgManager.FLOW_PREFIX)){
					IdName idName = new IdName(participant.substring(5),"",OrgManager.FLOW_PREFIX);
					idNameList.add(idName);
				}
			}
		}
		return idNameList;
	}
}
